﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.DataConvertLib;

namespace SerialPortDemo_04
{
    public partial class MainForm : Form
    {
        private MySerialPort serialPort = new MySerialPort();

        private int action = -1; // 0 读取; 1 请求解析读取

        // 按钮控制列表
        private List<Button> buttonList = new List<Button>();

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            serialPort.initBy(comboxPortName, comboBoxBaudRate);

            serialPort.showLogEvent += (message) => Invoke(new Action(() => textBoxLog.AppendText(message + "\r\n")));

            serialPort.receiveEvent += receive;

            // 按钮加入控制列表
            buttonList.Add(buttonRead);
            buttonList.Add(buttonRead1);
        }

        private void buttonRead_Click(object sender, EventArgs e)
        {
            try
            {
                // 按钮失效
                buttonList.ForEach(button => button.Enabled = false);

                action = 0;

                byte[] request = new byte[] { 0x01, 0x03, 0x00, 0x00, 0x00, 0x64 };

                serialPort.write(request);
            } catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void buttonOpen_Click(object sender, EventArgs e)
        {
            try
            {
                if (!serialPort.open)
                {
                    serialPort.openBy(comboxPortName.Text, int.Parse(comboBoxBaudRate.Text));

                    buttonOpen.Text = "关闭串口";
                }
                else
                {
                    serialPort.close();

                    buttonOpen.Text = "打开串口";
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void buttonRead1_Click(object sender, EventArgs e)
        {
            try
            {
                // 按钮失效
                buttonList.ForEach(button => button.Enabled = false);

                action = 1;
                byte[] request = new byte[] { 0x01, 0x03, 0x00, 0x01, 0x00, 0x03 };

                serialPort.write(request);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void receive(byte[] buffer)
        {
            try
            {
                switch (action)
                {
                    case 0:
                        MessageBox.Show("读取完成");
                        break;

                    case 1:
                        Invoke(new Action(() =>
                        {                            // 响应的数据
                            // 01 03 06 00 00 00 02 00 00 80 B5
                            // 0x01 -- 从机地址；  0x03 -- 功能码； 0x06 -- 数据长度
                            // 0x00 0x00 -- 第一个寄存器的值
                            // 0x00 0x02 -- 第二个寄存器的值
                            // 0x00 0x00 -- 第三个寄存器的值
                            // 0x80 0xB5 -- 校验位
                            textBoxAddress1.Text = ShortLib.GetShortFromByteArray(buffer, 3).ToString();
                            textBoxAddress2.Text = ShortLib.GetShortFromByteArray(buffer, 5).ToString();
                            textBoxAddress3.Text = ShortLib.GetShortFromByteArray(buffer, 7).ToString();
                        }));

                        MessageBox.Show("请求解析读取");
                        break;
                }

                action = -1;

                // 按钮恢复
                Invoke(new Action(() => buttonList.ForEach(button => button.Enabled = true)));

            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
